<template>
 <div class='chatnew'>
     <div class="chatnewheader" v-if="newchat.id">
         <slot />
     </div>
     <div class="chatnewheader" v-else>
         快去选择聊天对象，开始你的聊天把
     </div>
     <div class="centent">
         <div class="item" v-for="item in noreadmsglist" :key="item.id">
            
             <div class="other" v-if="item.authorid==newchat.id && item.toauthorid==$store.state.readerdata.id">
                 <div class="imgcover">
                     <img :src="newchat.headerimg" alt="">
                 </div>
                <span>{{item.msg}}</span>
             </div>

              <div class="me" v-if="item.toauthorid==newchat.id && item.authorid==$store.state.readerdata.id"> 
                <span>{{item.msg}}</span>
                 <div class="imgcover">
                     <img :src="$store.state.readerdata.headerimg" alt="">
                 </div>
             </div>
         </div>
     </div>
     <div class="footer" v-if="newchat.id">
         <input
            placeholder="请输入内容"
            v-model="textarea"
            @keyup.enter="putMessage">
            </input>
            <div class="right">
                <button @click="putMessage">发送</button>
                <button @click="cancel">取消</button>
            </div>
     </div>
 </div>
</template>

<script>
   export default {
      data() {
          return {
              textarea:'',
              chatMessageList:[]
          }
      },
      props:["newchat",'noreadmsglist'],
      watch:{
        //   noreadmsglist(newdata,olddata){
        //     //   console.log(newdata)
        //   }
      },
      //生命周期 - 创建完成
     created(){ 
         
       
     },

     //DOM挂载完毕
     mounted() {
         this.getMessageList()
     },
  
      methods: {
          //获取当前聊天对象的全部聊天信息
          getMessageList(){
              
          },
          //发送消息
          putMessage(){
            //   console.log(this.noreadmsglist)
              if(this.textarea.length==0){
                  return this.$message.success("太短了")
              }
             let item={}
             item.headerimg=this.$store.state.readerdata.headerimg
              item.authorid=this.$store.state.readerdata.id
              item.message=this.textarea 
            let con=io.connect("http://localhost:3001")
                con.emit("toUser",{
                data:{
                    authorid:this.$store.state.readerdata.id,
                    toauthorid:this.newchat.id,
                    time:Date.now(), 
                    msg:this.textarea
                }
                })
              
            //   this.chatMessageList.push(item) 
              this.textarea=""
              
          },

          //取消发送
          cancel(){
              this.textarea=""
          },

      },
 }
</script>

<style lang='less' scoped>
.chatnew {
    width: 100%;
    height: 100%;
    
  .chatnewheader {
      background-image: linear-gradient(to right, #4facfe 0%, #00f2fe 100%);
      color: #fff;
      min-height: 60px;
      font-size: 26px;
      font-weight: 700;
      text-align: center;
      line-height: 60px;
  }

  .centent {
      height: 400px;
      overflow: hidden;
      .item{
          min-height: 40px;   
          .me{
              float: right;
              
              width: 100%;
              
              padding: 5px 10px;
              display: flex;
                .imgcover{
                    width: 35px;
                    display: flex;
                    flex-direction: column;
                    justify-content: flex-start;
                    img{
                  width: 35px;
                  height: 35px;
                  display: inline-block;
                  border-radius: 50%;
              }
                }
              
              span{
                  margin-left: 10px;
                  flex:1;
                  display: block;
                  padding: 5px 20px; 
                background-color: #f2f2f2;
              }
          }
          .other{
              float: left;
               width: 100%;
              
              padding: 5px 10px;
              display: flex;

               .imgcover{
                    width: 35px;
                    display: flex;
                    flex-direction: column;
                    justify-content: flex-start;
                    img{
                  width: 35px;
                  height: 35px;
                  display: inline-block;
                  border-radius: 50%;
              }
                }
              span{
                   margin-right: 10px;
                  flex:1;
                  display: block;
                  padding: 5px 20px; 
                background-color: #f2f2f2;
              }
          }
      }
  }

  .footer {
      height: 100px;
      position: relative;
      input{
          width: 100%;
          height: 40px;
      }
    .right{
        position: absolute;
        right: 10px;
        bottom:0;
         button {
             display: inline-block;
             margin-left:10px;
             border:none;
             width: 80px;
             height: 30px;
             line-height: 30px;
             font-size: 16px;
             text-align: center;
             &:hover{
                 color:red;
                 background-color: #ccc;
             }
    }
    }

   
  }
}
</style>